package com.emode.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import org.apache.commons.lang.xwork.StringUtils;
import org.springframework.transaction.annotation.Transactional;

import com.emode.base.BaseDaoImpl;
import com.emode.entity.Bbstopic;
import com.emode.service.TopicService;
import com.emode.base.Pager;

@SuppressWarnings("unchecked")
public class TopicServiceImpl extends BaseDaoImpl<Bbstopic> implements TopicService{
	@Override
	public List findTitle(String topic) {
		String hql = "from Bbstopic topic where ttopic like ?";
		return ht.find(hql, new Object[]{"%"+topic+"%"});
	}

	@Override
	public int getTopicCount() {
		String hql = "select count(t) from Bbstopic t ";
		return getCount(hql);
	}

	@Override
	@Transactional
	public void updateClickCount(Integer integer) {
		String sql = "update Bbstopic t set t.tclickCount =t.tclickCount+1 where tid = ?";
		makeTransientByIds(sql,integer);
		
	}


	@Override
	public List findTopic(Integer integer,Pager p) {
		
		String hql = "from Bbstopic t where tsid= ? order by t.ttime desc";
		return PageQuery(hql, p,integer);
	}

	/*public List findTopicById(Integer integer) {
		String hql ="from Bbstopic t where t.tid = ?";
		return ht.find(hql,integer);
	}*/

	@Override
	public List findTopicByUid(Integer integer) {
		String hql = "from Bbstopic t where tuid= ?";
		return ht.find(hql,integer);
	}

	@Override
	public int countTopics(Integer integer) {
		String hql = "select count(t.ttopic) from Bbstopic t where tsid = ?";
		return getCount(hql,integer);
	}


	@Override
	public void updateTopic(Bbstopic topic) {
		String sql = "update Bbstopic t set t.tlastReply=?,t.treplyCount=t.treplyCount+1 where t.tid = ?";
		makeTransientByIds(sql,new Object[]{topic.getTlastReply(),topic.getTid()});
		
	}

	@Override
	public List allTopics(Pager p) {
		String hql = "from Bbstopic t ";
		return PageQuery(hql, p);
	}

	/* (non-Javadoc)
	 * @see com.emode.service.imp.ITopicService#findTopicWithPage(com.emode.entity.Bbstopic)
	 */
	@Override
	public List<Bbstopic> findTopicWithPage(Serializable id,Pager p) {
		if(id==null){
			throw new RuntimeException("条件查询为空！");
		}
		//组织条件查询
		String whereHql = "";
		//定义封装的条件查询
		List valueList = new ArrayList();
		if(id!=null){
		 	whereHql = " and tsid = ?";
			valueList.add(id);
		}
		Object[] values = valueList.toArray();
		//排序
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("o.ttime","desc ");
		return findObjectByConditionWithPage(whereHql, values, orderby, p);
	}

	/* (non-Javadoc)
	 * @see com.emode.service.imp.ITopicService#getCounts(java.io.Serializable)
	 */
	@Override
	public int getCounts(Serializable id) {
		//组织条件查询
		String whereHql = "";
		//定义封装的条件查询
		List valueList = new ArrayList();
		if(id!=null){
		whereHql = " and tsid = ?";
			valueList.add(id);
		}
		Object[] values = valueList.toArray();
		return getObjectCountsByid(whereHql, values);
	}

	/* (non-Javadoc)
	 * @see com.emode.service.imp.ITopicService#find(com.emode.entity.Bbstopic)
	 */
	@Override
	public Bbstopic find(Bbstopic topic) {
		//组织条件查询
		String whereHql = "";
		//定义封装的条件查询
		List valueList = new ArrayList();
		if(topic.getTid()!=null){
			whereHql = whereHql+" and o.tid = ?";
			valueList.add(topic.getTid());
		}
		if(StringUtils.isNotBlank(topic.getTtopic())){
			whereHql =whereHql+" and o.ttopic = ?";
			valueList.add("%"+topic.getTtopic()+"%");
		}
		System.out.println("----->wherehql:"+whereHql);
		Object[] values = valueList.toArray();
		List<Bbstopic> list = findObjectByConditionWithNoPage(whereHql, values);
		if(list.size()>0){
			return list.get(0);
		}
		return null;
	}

	@Override
	public List latestTopic(Pager p) {
		//通过发帖时间和最新回复确定topic的排序
		String hql = "FROM Bbstopic t ORDER BY TlastReply";
		return PageQuery(hql, p);
	}


}
